package com.tronsis.distribution.activity.me;

import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.LinearLayout;

import com.tronsis.distribution.R;
import com.tronsis.distribution.bean.UserBean;
import com.tronsis.distribution.biz.factory.UserBizFactory;
import com.tronsis.library.activity.ToolBarBaseActivity;
import com.tronsis.library.db.DBUtil;
import com.tronsis.library.http.UICallBack;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MessageSettingActivity extends ToolBarBaseActivity {


    @BindView(R.id.cbx_app_message)
    CheckBox mCbxAppMessage;
    @BindView(R.id.ll_coupons)
    LinearLayout mLlCoupons;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_setting);
        ButterKnife.bind(this);
        setTitle(true, getString(R.string.setting));

        UserBean loginUser = UserBizFactory.createBiz().getLoginUser(mContext);
        mCbxAppMessage.setChecked(loginUser.isAppNotice());
    }

    @OnClick(R.id.cbx_app_message)
    void cbxAppMessageClick() {
        boolean isChecked = mCbxAppMessage.isChecked();
        setAppNotice(isChecked);
    }

    private void setAppNotice(final boolean isChecked) {
        try {
            UserBizFactory.createBiz().updateInfo(this, isChecked, null, null, null, null, null, null, null, null, null, new UICallBack() {

                @Override
                public void onStart() {
                    showLoadingDialog();
                }

                @Override
                public void onRefresh(Object data) {
                    dismissLoadingDialog();
                    UserBean loginUser = UserBizFactory.createBiz().getLoginUser(mContext);
                    loginUser.setAppNotice(isChecked);
                    DBUtil.getInstance(mContext).update(loginUser);
                }

                @Override
                public void onFailure(int status) {
                    dismissLoadingDialog();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
